using TangMonk.Models;
using TangMonk.Service;

namespace TangMonk.Query;

public class UserQuery
{
    private readonly UserBaseService _baseService;

    public UserQuery(UserBaseService baseService)
    {
        _baseService = baseService;
    }

    public async Task<RequestUserResult> OneUser(string userId)
    {
        var result = new RequestUserResult(new List<User>(), new RequestResult("get user"));
        try
        {
            var user = await _baseService.GetOne(userId);
            result.Users.Add(user);
            result.Count = 1;
            result.BaseQueryResult.Success = true;
            result.BaseQueryResult.Result = "query success";
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            result.BaseQueryResult.Success = false;
            result.BaseQueryResult.Result = "query failed";
        }

        return result;
    }

    public Task<RequestUserResult> Users()
    {
        var users = _baseService.GetAll().Result;
        var result = new RequestUserResult(users, new RequestResult("get all users", true));
        return Task.FromResult(result);
    }

    public Task<RequestUserResult> UserLogin(UserLoginType type, string credential, string password)
    {
        return Task.FromResult(_baseService.Login(type, credential, password));
    }

}